package com.ly.mpayment.rest.system;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ly.mpayment.core.util.Config;
import com.ly.mpayment.core.util.DateUtils;
import com.ly.mpayment.core.util.JsonUtils;
import com.ly.mpayment.core.util.MapUtil;
import com.ly.mpayment.core.util.ResultMessage;
import com.ly.mpayment.persist.entity.ChannelBase;
import com.ly.mpayment.persist.entity.Operators;
import com.ly.mpayment.persist.service.channelbase.IChannelBaseService;
import com.ly.mpayment.persist.service.user.IUserService;
import com.ly.mpayment.rest.AbsRest;

import cn.jpush.api.utils.StringUtils;
import net.sf.json.JSONObject;

@Component
@Path("system/channelbase/")
public class ChannelBaseRest extends AbsRest{
	@Autowired
	IUserService userService;

	@Autowired
	IChannelBaseService channelBase;

	@Path("findPageChannel")
	@POST
	public String findPageChannel(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,"start","pageSize","baseName","operatoruid");
		if(result == null){
			Map<String, Object> parameters = MapUtil.getMap();
			JSONObject json = JsonUtils.converToJson(data);
			if(!StringUtils.isEmpty(json.getString("baseName"))){
				parameters.put(":baseName", json.getString("baseName").concat("%"));
			}
			if(!StringUtils.isEmpty(json.getString("operatoruid"))){
				parameters.put(":operatoruid", json.getString("operatoruid"));
			}
			return ResultMessage.getSucceed( JsonUtils.excludes(this.channelBase.findPageChannel(parameters, json.getInt("start"), json.getInt("pageSize")),
					new String[]{"remark","code"}));
		}
		return result;
	}

	@Path("findByChannel")
	@POST
	public String findByChannel(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,"operatoruid");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			return ResultMessage.getSucceed( JsonUtils.jsonArrayToJson(this.channelBase.findByChannel(json.getString("operatoruid")),
					new String[]{"remark","code","operatorsBase"}));
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("addChannel")
	@POST
	public String addChannel(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,"operatoruid","baseName","basePrice","flow");
		if(result == null){
			 JSONObject json = JSONObject.fromObject(data);
			 ChannelBase base = new ChannelBase();
			 base.setBaseName(json.getString("baseName"));
			 base.setStatus(Config.publicStatus[1]);
			 base.setCreatetime(DateUtils.getNowDate());
			 base.setBasePrice(new BigDecimal(json.getString("basePrice")));
			 base.setFlow(Integer.valueOf(json.getString("flow")));
			 base.setOperatorsBase(new Operators(json.getString("operatoruid")));
			 if(this.channelBase.saveChannel(base) != null){
				 return ResultMessage.getSucceed(null);
			 }
			 return ResultMessage.getFailt("");

		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("updateChannel")
	@POST
	public String updateChannel(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,"operatoruid","baseName","baseId","basePrice","flow");
		if(result == null){
			 JSONObject json = JSONObject.fromObject(data);
			 ChannelBase base = this.channelBase.findById(json.getString("baseId").trim());
			 base.setBaseName(json.getString("baseName"));
			 base.setOperatorsBase(new Operators(json.getString("operatoruid")));
			 base.setBasePrice(new BigDecimal(json.getString("basePrice")));
			 base.setFlow(Integer.valueOf(json.getString("flow")));
			 if(this.channelBase.updateChannel(base) != null){
				 return ResultMessage.getSucceed(null);
			 }
			 return ResultMessage.getFailt("");
		}
		return result;
	}

	/**
	 * 查询所有面值
	 * @param data
	 * @param request
	 * @return
	 */
	@Path("findAllChannel")
	@POST
	public String findAllChannel(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,"");
		if(result == null){
			JSONObject json = JsonUtils.converToJson(data);
			return ResultMessage.getSucceed( JsonUtils.jsonArrayToJson(this.channelBase.findAllChannel(),
					new String[]{"remark","code","operatorsBase"}));
		}
		return result;
	}

	@SuppressWarnings("static-access")
	@Path("deleteChannel")
	@POST
	public String deleteChannel(String data,@Context HttpServletRequest request){
		String result = this.validationParameter(data,request,userService,"baseName","baseId");
		if(result == null){
			JSONObject json = JSONObject.fromObject(data);
			ChannelBase base = this.channelBase.findById(json.getString("baseId").trim());
			if(this.channelBase.deleteChannel(base)){
				return ResultMessage.getSucceed(null);
			}
			return ResultMessage.getFailt("");
		}
		return result;
	}

}
